/*
 * Copyright 2012-2014 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jqd.framework.core.hessian.biz.action.support;

import com.jqd.framework.core.hessian.model.ServiceRequest;
import com.jqd.framework.core.spring.context.ApplicationContextFactory;

/**
 * 
 * 该映射类允许用户不通过任何配置及注解可以直接通过Controller中设置的actionId找到相应的Action
 * 
 * @author liaozhicheng.cn@gmail.com
 * @date 2012-6-11
 * @since 1.0
 */
public class BeanMethodNameActionHandlerMapping extends AbstractActionHandlerMapping {
	
	@Override
	protected Object getHandlerInternal(ServiceRequest serviceRequest) {
		
		String actionId = serviceRequest.getRequestedActionID();
		
		// controller中设置的actionId必须包含至少一个'.'字符，并将最后一个'.'前面的字符串当做
		// spring中配置的bean name属性，最后一个'.'后面的部分对应为需要调用的方法名
		if (BeanMethodNameActionIdUtils.valid(actionId)) {
			String beanName = BeanMethodNameActionIdUtils.getBeanName(actionId);
			
			try {
				Object handler = ApplicationContextFactory.getCurrentApplicationContext().getBean(
						beanName);
				if (handler != null) {
					// 找到对应的动作处理器之后，将该映射保存缓存
					registerHandler(actionId, handler);
					logInfo(actionId, handler);
					return handler;
				}
			}
			catch (Exception ex) {
				// is OK, catch BeansException, can not found bean with name beanName
				return null;
			}
		}
		return null;
	}
	
}
